package top.happylaishop.shop.utils;

import cn.hutool.core.util.StrUtil;
import org.springframework.core.env.ConfigurableEnvironment;
import top.happylaishop.shop.common.Const;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

public class CommonUtils {

    /**
     * line separator
     */
    public static final String LINE_SEPARATOR = System.getProperty("line.separator");
    /**
     * The separator of property name
     */
    public static final String PROPERTY_NAME_SEPARATOR = ".";
    /**
     * The prefix of property name of HappyLaishop
     */
    public static final String HAPPYLAISHOP_PREFIX = "happylaishop";

    public static final String SPRING_APPLICATION_NAME_PROPERTY = "spring.application.name";

    public static final String HAPPYLAISHOP_APPLICATION_NAME_PROPERTY = "happylaishop.application.name";

    public static final String HAPPYLAISHOP_APPLICATION_QOS_ENABLE_PROPERTY = "happylaishop.application.qos-enable";

    public static final String HAPPYLAISHOP_CONFIG_MULTIPLE_PROPERTY = "happylaishop.config.multiple";

    /**
     * The prefix of property name for Dubbo scan
     */
    public static final String HAPPYLAISHOP_SCAN_PREFIX = HAPPYLAISHOP_PREFIX + PROPERTY_NAME_SEPARATOR + "scan" + PROPERTY_NAME_SEPARATOR;

    /**
     * The prefix of property name for Dubbo Config
     */
    public static final String HAPPYLAISHOP_CONFIG_PREFIX = HAPPYLAISHOP_PREFIX + PROPERTY_NAME_SEPARATOR + "config" + PROPERTY_NAME_SEPARATOR;

    public static final String BASE_PACKAGES_PROPERTY_RESOLVER_BEAN_NAME = "happylaishopScanBasePackagesPropertyResolver";

    /**
     * The property name of base packages to scan
     * <p>
     * The default value is empty set.
     */
    public static final String BASE_PACKAGES_PROPERTY_NAME = "base-packages";

    public static SortedMap<String, Object> filterHappyLaishopProperties(ConfigurableEnvironment environment) {

        SortedMap<String, Object> dubboProperties = new TreeMap<>();

        Map<String, Object> properties = EnvironmentUtils.extractProperties(environment);

        for (Map.Entry<String, Object> entry : properties.entrySet()) {
            String propertyName = entry.getKey();

            if (propertyName.startsWith(HAPPYLAISHOP_PREFIX + PROPERTY_NAME_SEPARATOR)
                    && entry.getValue() != null) {
                dubboProperties.put(propertyName, environment.resolvePlaceholders(entry.getValue().toString()));
            }

        }

        return Collections.unmodifiableSortedMap(dubboProperties);
    }

    public static String getToken(){
        String token = ServletUtils.getRequest().getHeader(Const.HEADER);
        if (StrUtil.isNotEmpty(token)) {
            if(token.startsWith(Const.TOKEN_PREFIX)){
                token = token.replace(Const.TOKEN_PREFIX, "");
            } else if(token.trim().equals(Const.TOKEN_PREFIX.trim())){
                token = "";
            }
        }
        return token;
    }

    public static String generateLoginToken(String token) {
        return Const.TOKEN_USER_TOKEN + token;
    }

    public static String getCacheUserKey() {
        return generateLoginToken(getToken());
    }

    public static BigDecimal mul(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.multiply(b2);
    }

    public static BigDecimal add(BigDecimal cartTotalPrice, BigDecimal productTotalPrice) {
        return initBigDecimal(cartTotalPrice).add(productTotalPrice);
    }

    public static BigDecimal initBigDecimal(BigDecimal bigDecimal){
        return bigDecimal == null ? bigDecimal : BigDecimal.ZERO;
    }
}
